﻿/****************************************************************************
** Copyright (C) 2017 ZhouLiQiu(zhouliqiu@126.com).
**
** This file is part of the qtc_astyle, It is a plugin for Qt Creator.
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
** THE SOFTWARE.
**
****************************************************************************/
#include "formstyleoption.h"

#include "astyleconstants.h"
#include "astyleactions.h"

#include <coreplugin/icore.h>

#include <texteditor/codestyleeditor.h>
#include <texteditor/displaysettings.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/textdocument.h>

static const char *styleString[] = {
    ""
    ,
    "int Foo(bool isBar)\n"
    "{\n"
    "    if (isBar)\n"
    "    {\n"
    "        bar();\n"
    "        return 1;\n"
    "    }\n"
    "    else\n"
    "        return 0;\n"
    "}\n"
    ,
    "int Foo(bool isBar) {\n"
    "    if (isBar) {\n"
    "        bar();\n"
    "        return 1;\n"
    "    } else\n"
    "        return 0;\n"
    "}\n"
    ,
    "int Foo(bool isBar) \n"
    "{\n"
    "    if (isBar) {\n"
    "        bar();\n"
    "        return 1;\n"
    "    } else\n"
    "        return 0;\n"
    "}\n"
    ,
    "int Foo(bool isBar)\n"
    "{\n"
    "     if (isBar) {\n"
    "          bar();\n"
    "          return 1;\n"
    "     } else\n"
    "          return 0;\n"
    "}\n"
    ,
    "int Foo(bool isBar)\n"
    "    {\n"
    "    if (isBar)\n"
    "        {\n"
    "        bar();\n"
    "        return 1;\n"
    "        }\n"
    "    else\n"
    "        return 0;\n"
    "    }\n"
    ,
    "int Foo(bool isBar) {\n"
    "    if (isBar) {\n"
    "        bar();\n"
    "        return 1;\n"
    "        }\n"
    "    else\n"
    "        return 0;\n"
    "    }\n"
    ,
    "int Foo(bool isBar)\n"
    "{\n"
    "  if (isBar)\n"
    "    {\n"
    "      bar();\n"
    "      return 1;\n"
    "    }\n"
    "  else\n"
    "    return 0;\n"
    "}\n"
    ,
    "int Foo(bool isBar)\n"
    "{\n"
    "        if (isFoo) {\n"
    "                bar();\n"
    "                return 1;\n"
    "        } else\n"
    "                return 0;\n"
    "}\n"
    ,
    "int Foo(bool isBar)\n"
    "{  if (isBar)\n"
    "   {  bar();\n"
    "      return 1;\n"
    "   } else\n"
    "      return 0;\n"
    "}\n"
    ,
    "int Foo(bool isBar)\n"
    "{\n"
    "    if (isFoo) {\n"
    "        bar();\n"
    "        return 1;\n"
    "    } else {\n"
    "        return 0;\n"
    "    }\n"
    "}\n"
    ,
    "int Foo(bool isBar)\n"
    "{  if (isBar)\n"
    "   {  bar();\n"
    "      return 1; }\n"
    "    else\n"
    "      return 0; }\n"
    ,
    "int Foo(bool isBar) {\n"
    "    if (isBar) {\n"
    "        bar()\n"
    "        return 1; }\n"
    "    else\n"
    "        return 0; }\n"
    ,
    "int Foo(bool isBar) {\n"
    "    if (isBar) {\n"
    "        bar();\n"
    "        return 1;\n"
    "    } else\n"
    "        return 0;\n"
    "}\n"
};

using namespace TextEditor;

namespace AStyle {
namespace Internal {

FormStyleOption::FormStyleOption(QObject *parent) :
    Core::IOptionsPage(parent)
{
    setId(AStyle::Constants::OPTIONS_STYLE_ID);
    setCategory("Artistic Style");
    setDisplayCategory(QString::fromLocal8Bit("代码格式化"));
    setDisplayName(QString::fromLocal8Bit("样式"));
    setCategoryIcon(Utils::Icon(":/res/as.png"));
}

QWidget *FormStyleOption::widget()
{
    if (!m_widget)
    {
        m_widget = new QWidget;
        m_ui.setupUi(m_widget);

        m_ui.editStyle->textDocument()->setFontSettings(TextEditorSettings::fontSettings());
        connect(m_ui.cmbStyle, SIGNAL(currentIndexChanged(int)), this, SLOT(setStyleText(int)));

        DisplaySettings ds = m_ui.editStyle->displaySettings();
        ds.m_visualizeWhitespace = true;
        ds.m_highlightCurrentLine = true;
        m_ui.editStyle->setDisplaySettings(ds);

        m_group.clear();

        m_group.insert(theAStyleSetting(Style), m_ui.cmbStyle);
    }
    return m_widget;
}

void FormStyleOption::setStyleText(int index)
{
    m_ui.editStyle->setPlainText(QLatin1String(styleString[index]));
}

void FormStyleOption::apply()
{
    m_group.apply(Core::ICore::settings());
}

void FormStyleOption::finish()
{
    m_group.finish();
    delete m_widget;
}

} // namespace Internal
} // namespace AStyle
